package microcredit.gui.editor.training;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.util.FixedLengthDocument;
import microcredit.gui.util.NumericTextField;
import microcredit.gui.util.UIUtil;
import microcredit.model.TrainingType;
import microcredit.model.domain.Status;
import net.miginfocom.swing.MigLayout;

public class TrainingTypeGeneralForm extends AbstractEditorForm<TrainingType> {
	
	private JLabel lblShortname;
	private JLabel lblName;
	private JTextField shortNameTextField;
	private JTextField nameTextField;
	private JLabel lblMaximumMembers;
	private NumericTextField maximumMembersTextField;
	private JCheckBox unlimitedCheckBox;
	private JCheckBox statusCheckBox;
	private JLabel lblGeneralInformation;
	private JScrollPane scrollPane;
	private JTextArea generalInformationTextArea;
	
	public TrainingTypeGeneralForm() {
		initialize();
		setTitle("General");
		UIUtil.setMandatoryFields(this, TrainingType.SHORT_NAME, TrainingType.LONG_NAME);
	}
	
	@Override
	public void focusDefault() {
		getShortNameTextField().requestFocus();
		getShortNameTextField().selectAll();
	}
	
	@Override
	public void apply(TrainingType e) throws Throwable {
		getShortNameTextField().setText(e.getShortName());
		getNameTextField().setText(e.getLongName());
		getStatusCheckBox().setSelected(e.getStatus() == Status.A);
		
		getUnlimitedCheckBox().setSelected(e.getMaximumMembers() == null);
		if (e.getMaximumMembers() != null) {
			getMaximumMembersTextField().setText(String.valueOf(e.getMaximumMembers()));
			getMaximumMembersTextField().setEnabled(true);
			getMaximumMembersTextField().setEditable(true);
		} else {
			getMaximumMembersTextField().setText("");
			getMaximumMembersTextField().setEnabled(false);
			getMaximumMembersTextField().setEditable(false);
		}
		getGeneralInformationTextArea().setText(e.getDescription());
	}

	@Override
	public TrainingType receive(TrainingType e) throws Throwable {
		e.setShortName(getShortNameTextField().getText());
		e.setLongName(getNameTextField().getText());
		e.setStatus(getStatusCheckBox().isSelected() ? Status.A : Status.I);
		e.setDescription(getGeneralInformationTextArea().getText());
		if (getUnlimitedCheckBox().isSelected()) {
			e.setMaximumMembers(null);
		} else {
			try {
				Number n = getMaximumMembersTextField().getNumberValue();
				if (n != null) {
					e.setMaximumMembers(n.longValue());
				}
			} catch (Exception e1) {
				e.setMaximumMembers(null);
			}
		}
		return e;
	}

	private void unlimitedChanged() {
		if (getUnlimitedCheckBox().isSelected()) {
			getMaximumMembersTextField().setText("");
			getMaximumMembersTextField().setEnabled(false);
			getMaximumMembersTextField().setEditable(false);
		} else {
			getMaximumMembersTextField().setText("");
			getMaximumMembersTextField().setEnabled(true);
			getMaximumMembersTextField().setEditable(true);
		}
	}
	
	private void initialize() {
		setLayout(new MigLayout("", "[][][grow]", "[][][][][][][grow]"));
		add(getStatusCheckBox(), "cell 2 1");
		add(getLblShortname(), "cell 1 2,alignx trailing");
		add(getShortNameTextField(), "cell 2 2,growx");
		add(getLblName(), "cell 1 3,alignx trailing");
		add(getNameTextField(), "cell 2 3,growx");
		add(getLblMaximumMembers(), "cell 1 4,alignx trailing");
		add(getMaximumMembersTextField(), "flowx,cell 2 4");
		add(getUnlimitedCheckBox(), "cell 2 4,pushx ");
		add(getLblGeneralInformation(), "cell 1 6,aligny top");
		add(getScrollPane(), "cell 2 6,grow");
	}

	private JLabel getLblShortname() {
		if (lblShortname == null) {
			lblShortname = new JLabel("Short name");
			lblShortname.setLabelFor(getShortNameTextField());
		}
		return lblShortname;
	}
	private JLabel getLblName() {
		if (lblName == null) {
			lblName = new JLabel("Name");
			lblName.setLabelFor(getNameTextField());
		}
		return lblName;
	}
	private JTextField getShortNameTextField() {
		if (shortNameTextField == null) {
			shortNameTextField = new JTextField();
			shortNameTextField.setColumns(10);
			shortNameTextField.setDocument(new FixedLengthDocument(15));
			shortNameTextField.setName(TrainingType.SHORT_NAME);
		}
		return shortNameTextField;
	}
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setColumns(10);
			nameTextField.setDocument(new FixedLengthDocument(255));
			nameTextField.setName(TrainingType.LONG_NAME);
		}
		return nameTextField;
	}
	private JLabel getLblMaximumMembers() {
		if (lblMaximumMembers == null) {
			lblMaximumMembers = new JLabel("Maximum members");
			lblMaximumMembers.setLabelFor(getMaximumMembersTextField());
		}
		return lblMaximumMembers;
	}
	private NumericTextField getMaximumMembersTextField() {
		if (maximumMembersTextField == null) {
			maximumMembersTextField = new NumericTextField();
			maximumMembersTextField.setEnabled(false);
			maximumMembersTextField.setEditable(false);
			maximumMembersTextField.setColumns(5);
			maximumMembersTextField.setName(TrainingType.MAXIMUM_MEMBERS);
			maximumMembersTextField.setFormat((DecimalFormat)DecimalFormat.getIntegerInstance());
		}
		return maximumMembersTextField;
	}
	private JCheckBox getUnlimitedCheckBox() {
		if (unlimitedCheckBox == null) {
			unlimitedCheckBox = new JCheckBox("Unlimited");
			unlimitedCheckBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					unlimitedChanged();
				}
			});
			unlimitedCheckBox.setSelected(true);
		}
		return unlimitedCheckBox;
	}
	private JCheckBox getStatusCheckBox() {
		if (statusCheckBox == null) {
			statusCheckBox = new JCheckBox("Active");
			statusCheckBox.setSelected(true);
		}
		return statusCheckBox;
	}
	private JLabel getLblGeneralInformation() {
		if (lblGeneralInformation == null) {
			lblGeneralInformation = new JLabel("General information");
		}
		return lblGeneralInformation;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getGeneralInformationTextArea());
		}
		return scrollPane;
	}
	private JTextArea getGeneralInformationTextArea() {
		if (generalInformationTextArea == null) {
			generalInformationTextArea = new JTextArea();
			generalInformationTextArea.setDocument(new FixedLengthDocument(5000));
		}
		return generalInformationTextArea;
	}
}
